import { AppCTX } from "../../types/koaExtend";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import pageMW from "../../middleware/pager";
import { getUserByMobile } from "../../service/userService";
import {
  listBrandAdminWithUsersWithoutBrandOwnerByBrandIDAndUserID,
  becomeBrandAdmin,
  becomeBrandAdminCachelly,
  updateBrandAdmin,
  cancelBrandAdmin,
  cancelBrandAdminCachelly,
  identifiyBrandAdminEmployee,
} from "../../service/brandAdminService";
import { canUse, isUndefined, toString, _pick } from "mmb";
const router = createBrandApiMWRouter();

// 获取品牌管理员列表
router.get(
  "brand-admins",
  "/brd/:brandId/admins",
  pageMW,
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.params;
    const { userId, limit, offset } = ctx;
    //1 userId(操作人不是品牌管理员),报错
    const {
      total,
      items,
    } = await listBrandAdminWithUsersWithoutBrandOwnerByBrandIDAndUserID(
      brandId,
      userId,
      { limit, offset }
    );

    ctx.body = {
      total,
      rows: items.map((ba) => ({
        ..._pick(ba, "brandAdminId", "brandId", "aliasName", "userNickName", "userPic"),
      })),
    };
  }
);

// 新增品牌管理员
router.post("/brd/:brandId/admin", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { mobile } = ctx.request.body;
  const { userId } = ctx;

  //1 增加判断如果该手机号对应的用户已经是管理员,报错
  if (!canUse(mobile)) {
    ctx.throw(400, "mobile can not use");
  }
  const theUser = await getUserByMobile(mobile);
  if (isUndefined(theUser)) {
    ctx.throw(400, "mobile can not find user");
  }

  // todo 可以在判断用户是否是某个品牌管理员的缓存机制完成后加入

  const brandAdmin = await becomeBrandAdminCachelly({
    userId: theUser.userId,
    brandId: brandId,
    aliasName: toString(theUser.nickName || theUser.mobile),
    became: Date.now(),
  });

  ctx.body = {
    ..._pick(brandAdmin, "brandAdminId", "brandId", "aliasName"),
  };
});

// 移除品牌管理员
router.delete("/brd/:brandId/admin/:brandAdminId", async (ctx: AppCTX, next) => {
  const { brandId, brandAdminId } = ctx.params;
  const { userId } = ctx;
  
  const brandAdmin = await cancelBrandAdminCachelly({brandAdminId})

  ctx.body = {
    ..._pick(brandAdmin, "brandAdminId", "brandId", "aliasName"),
  };
});

// 备注品牌管理员名称
router.put("/brd/:brandId/admin/alias/:brandAdminId", async (ctx: AppCTX, next) => {
  const { brandId, brandAdminId } = ctx.params;
  const { aliasName } = ctx.request.body;
  const { userId } = ctx;

  const brandAdmin = await updateBrandAdmin({
    brandAdminId,
    aliasName,
  });

  ctx.body = {
    ..._pick(brandAdmin, "brandAdminId", "brandId", "aliasName"),
  };
});

// 识别管理员
router.put("/brd/:brandId/admin/identify/:brandAdminId", async (ctx: AppCTX, next) => {
  const { brandId, brandAdminId } = ctx.params;
  const { aliasName } = ctx.request.body;
  const { userId } = ctx;

  const brandAdmin = await identifiyBrandAdminEmployee(brandAdminId);

  ctx.body = {
    ..._pick(brandAdmin, "brandAdminId", "brandId", "aliasName"),
  };
});

export default router;
